連線RDS後,查詢目前的RDS中有什麼,-U postgres是資料庫的使用者 -d postgres是預設的資料庫名稱
psql -h your-rds-endpoint -U postgres -d postgres
postgres-> CREATE DATABASE fight_web_game; #建立fight_web_game資料庫
postgres-> \l #列出所有可用的資料庫
List of databases
Name | Owner | Encoding | Collate | Ctype | ICU Locale | Locale Provider | Access privileges
----------------+----------+----------+-------------+-------------+------------+-----------------+----------------------
-
fight_web_game | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | libc |
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | libc |
rdsadmin | rdsadmin | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | libc | rdsadmin=CTc/rdsadmin
template0 | rdsadmin | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | libc | =c/rdsadmin
+
| | | | | | | rdsadmin=CTc/rdsadmin
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | libc | =c/postgres
+
| | | | | | | postgres=CTc/postgres
(5 rows)
接下來把導入資料到RDS
\q #退出 psql,回到EC2的命令行
psql --h your-rds-endpoint -U postgres -d fight_web_game < /home/ec2-user/database_dump.sql
提示錯誤,似乎是編碼錯誤,PostgreSQL預期接收UTF-8編碼:
ERROR: invalid byte sequence for encoding "UTF8": 0xff
檢查編碼,可以看到是編碼是UTF-16LE:
file -i /home/ec2-user/database_dump.sql
/home/ec2-user/database_dump.sql: text/plain; charset=utf-16le
hexdump -C -n 16 /home/ec2-user/database_dump.sql
00000000 ff fe 2d 00 2d 00 0d 00 0a 00 2d 00 2d 00 20 00 |..-.-.....-.-. .|
00000010
將編碼轉換成UTF-8,並且再度送出一次:
iconv -f UTF-16LE -t UTF-8 /home/ec2-user/database_dump.sql > /home/ec2-user/database_dump_utf8.sql
file -i /home/ec2-user/database_dump_utf8.sql #確認目前的編碼
psql --h your-rds-endpoint -U postgres -d fight_web_game < /home/ec2-user/database_dump_utf8.sql #改用database_dump_utf8.sql
可以看到大部分成功了,但有一個Error,暫時還沒解決,明天繼續:
SET
... (一些SET)
CREATE TABLE
ALTER TABLE
....(一些CREATE TABLE和ALTER TABLE)
ERROR: value too long for type character(1)
CONTEXT: COPY characters, line 1, column avatar_char: "orange"
...(一些row)
ALTER TABLE
...(一些ALTER TABLE)